﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>角色管理</title>
    <link href="~/Content/css/fontawesome/css/font-awesome.min.css" rel="stylesheet" />
    <link href="~/Content/easyui/themes/default/easyui.css" rel="stylesheet" />
    <link href="~/Content/easyui/themes/icon.css" rel="stylesheet" />
    <!--修改过的bootstrap-->
    <link href="~/Content/css/bootstrapmin.css" rel="stylesheet" />
</head>
<body>
    <div class="easyui-layout" fit="true">
        @*右边导航栏*@
        <div data-options="region:'east',split:true" style="width: 400px;">
            <div class="easyui-layout" fit="true">
                @*头部导航栏*@
                <div data-options="region:'north',split:true" style="height: 50px;line-height:45px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-save" id="saveRoleModule">保存</a>
                </div>
                @*中间内容*@
                <div data-options="region:'center',border:false,split:false">
                    <div id="tree">
                    </div>
                </div>
            </div>
        </div>
        @*中间内容*@
        <div data-options="region:'center',border:false,split:false">
            <div id="maingrid">
            </div>
        </div>
    </div>
    <div id="maindialog" style="padding:10px;">
        <form class="form-horizontal">
            <div class="form-group">
                <label for="txtRoleName" class="col-md-2 control-label">角色名称：</label>
                <div class="col-md-4">
                    <input type="text" class="form-control" id="txtRoleName" />
                </div>
                <label for="ddlIsEnable" class="col-md-2 control-label">是否可用：</label>
                <div class="col-md-4">
                    <select class="form-control" id="ddlIsEnable">
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="txtRoleDesc" class="col-md-2 control-label">角色描述： </label>
                <div class="col-md-10">
                    <textarea class="form-control" id="txtRoleDesc" rows="2"></textarea>
                </div>
            </div>
        </form>
    </div>
    <script src="~/Content/js/jquery-2.2.3.min.js"></script>
    <script src="~/Content/easyui/jquery.easyui.min.js"></script>
    <script src="~/Content/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script>
        $(function () {
            var parentId = 0;
            bindgrid();
            bindtree(parentId,0);
            binddialog();

            bindValidate();

            $("#saveRoleModule").on("click", function () {
                saveRoleModule();
            });
        });
        function bindgrid() {
            $("#maingrid").datagrid({
                nowrap: true,
                fit: true,
                fitColumns: true,
                autoRowHeight: true,
                striped: true,//是否显示斑马线效果
                //singleSelect: true, //是否單選
                //collapsible: true,//是否可折叠
                pagination: true, //分頁控制
                pageSize:20,
                rownumbers: true, //行號
                //showFooter: true,
                method: 'get',
                url: '/RoleManage/GetRoles?t=' + Math.random(),
                loadMsg: '数据正在加载...',
                columns: [[
                    { field: 'ck', checkbox: true },
                    { field: 'RoleId', title: '模组ID', width: 100 },
                    { field: 'RoleName', title: '模组名字', width: 100 },
                    { field: 'RoleDesc', title: '模组描述', width: 130 },
                    {
                        field: 'IsEnable', title: '是否可用', width: 60, formatter: function (value, row, index) {
                            return value=="1"?"是":"否";
                        }
                    }
                ]],
                onSelect: function (rowIndex, rowData) {
                    bindtree(0,rowData.RoleId);
                },
                onUnselect: function (rowIndex, rowData) {
                    bindtree(0, 0);
                },
                toolbar: [{
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        $("#maindialog").dialog("open");
                        $("#txtRoleName").val("");
                        $("#txtRoleDesc").val("");
                        $("#ddlIsEnable").val("1");
                    }
                }, '-', {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var rowData = $('#maingrid').datagrid("getSelected");
                        if (rowData != null) {
                            $("#maindialog").dialog("open");
                            $("#maindialog").data("RoleId", rowData.RoleId);
                            $("#txtRoleName").val(rowData.RoleName);
                            $("#txtRoleDesc").val(rowData.RoleDesc);
                            $("#ddlIsEnable").val(rowData.IsEnable);
                        } else {
                            alertMsg("请至少选择一项！");
                        }
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var rows = $('#maingrid').datagrid("getSelections");
                        if (rows.length > 0) {
                            $.messager.confirm('确认对话框', '确定要删除这条记录？', function (r) {
                                if (r) {
                                    deleteRole(rows);
                                }
                            });
                        } else {
                            alertMsg("请至少选择一项！");
                        }
                    }
                }, '-', {
                    text: '启用/弃用',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var rowData = $('#maingrid').datagrid("getSelected");
                        if (rowData != null) {
                            $.messager.confirm('确认对话框', '确定要修改这条记录？', function (r) {
                                if (r) {
                                    isEnableRole(rowData);
                                }
                            });
                        } else {
                            alertMsg("请至少选择一项！");
                        }
                    }
                }]
            });
        }
        function bindtree(parentId, roleId) {
            $('#tree').tree({
                method: "get",
                checkbox: true,
                url: "/RoleManage/GetSysRolesTree?parentId=" + parentId + '&roleId=' + roleId + '&t=' + Math.random()
                , onClick: function (node) {
                    
                }
            });
        }
        function binddialog() {
            $("#maindialog").dialog({
                title: "保存模组",
                //top: 10,
                //left: ($(window).width() - 600) / 2,
                width: 700,
                height: 280,
                inline: true, //定义如何布局窗口，如果设置为true，窗口将显示在它的父容器中，否则将显示在所有元素的上面
                iconCls: 'icon-save',
                modal: true,
                draggable: true, //定义是否能够拖拽窗口。
                closed: true,
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-ok',
                    handler: function () {
                        saveRole();
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $("#maindialog").dialog("close");
                        $('#maingrid').datagrid("reload");
                    }
                }]
            });
        }
        function bindValidate() {
            $("#txtRoleName").validatebox({
                required: true,
                validType:"length[1,20]",
                missingMessage: "角色名称不能为空",
            });
        }
        function isValid() {
            return $("#txtRoleName").validatebox("isValid");
        }
        function saveRole() {
            if (!isValid()) { return false;}
            var RoleId=$("#maindialog").data("RoleId");
            var RoleName = $("#txtRoleName").val();
            var RoleDesc = $("#txtRoleDesc").val();
            var IsEnable = $("#ddlIsEnable").val();
            var obj = {
                RoleId:RoleId,
                RoleName: RoleName,
                RoleDesc: RoleDesc,
                IsEnable: IsEnable
            };
            $.post("/RoleManage/Add", obj, function (data) {
                if (data.CodeId == "100") {
                    window.location.reload();
                } else {
                    alertMsg("保存失败！");
                }
            });
            return true;
        }
        function deleteRole(rows) {
            if (rows.length > 0) {
                var ids = "";
                for (var i = 0; i < rows.length; i++) {
                    ids = ids + rows[i].RoleId + ",";
                }
                $.post("/RoleManage/Delete", {
                    ids: ids
                }, function (data) {
                    if (data.CodeId == "100") {
                        window.location.reload();
                    } else {
                        alertMsg("删除失败！");
                    }
                });

            }
        }
        function isEnableRole(row) {
            if (row) {
                $.post("/RoleManage/UpdateIsEnable", {
                    RoleId: row.RoleId
                }, function (data) {
                    if (data.CodeId == "100") {
                        $("#maingrid").datagrid("reload");
                    } else {
                        alertMsg("修改失败！");
                    }
                });

            }
        }

        function saveRoleModule() {
            var rowData = $('#maingrid').datagrid("getSelected");
            var roleId = rowData.RoleId;
            var treeNodes = $('#tree').tree('getChecked',['checked','indeterminate']);
            var ids = [];
            for (var i = 0; i < treeNodes.length; i++) {
                ids.push(treeNodes[i].id);
            }
            $.post("/RoleManage/SaveRoleModule", {
                RoleId: roleId,
                Modules:ids.join(',')
            }, function (data) {
                if (data.CodeId == "100") {
                    //$("#maingrid").datagrid("reload");
                    alertMsg("保存成功！");
                } else {
                    alertMsg("保存失败！");
                }
            });
        }

        function alertMsg(msg) {
            $.messager.alert('提示', msg, 'info');
        }
    </script>
</body>
</html>
